package com.zhangmeng.tools.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.Resource;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.setting.Setting;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.ImagePath;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-03-09 16:24
 */
@Slf4j
public class MailController {

    @FXML
    public TextField host;

    @FXML
    public TextField port;

    @FXML
    public TextField from;

    @FXML
    public TextField user;

    @FXML
    public PasswordField pass;

    @FXML
    public TextArea content;

    @FXML
    public Button send;

    @FXML
    public ListView<String> user_list;

    @FXML
    public TextField to;

    @FXML
    public Button add_user_to;

    @FXML
    public TextField file_path;

    @FXML
    public TextField subject;

    @FXML
    public Button choose_file;

    public static int mail_port = 465;

    public ObservableList<String> list = FXCollections.observableArrayList();

    public static final String p = "smtp";

    private SimpleObjectProperty<File> file_info = new SimpleObjectProperty<>();

    //pa = ufinynhrjynkjfec

    @FXML
    public void initialize() {

        choose_file.setText(null);
        ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
        iv.setPreserveRatio(true);
        iv.setFitWidth(18);
        choose_file.setGraphic(iv);

        choose_file.setOnAction(event -> {
            Stage stage = new Stage();
            FileChooser fc = new FileChooser();
            fc.setTitle("单选文件");
            fc.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("所有类型", "*.*")
            );

            File file = fc.showOpenDialog(stage);

            if (file == null) {
                return;
            }
            file_info.setValue(file);
        });

        //设置端口默认
        if (port.getText().length() == 0) {
            port.setText(String.valueOf(MailController.mail_port));
        }

        user_list.setFixedCellSize(40);
        user_list.setItems(list);
        user_list.setPlaceholder(new Label("暂无发送人!"));

        from.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                host.setText(p + "." + newValue.split("@")[1]);
                user.setText(newValue.split("@")[0]);
            }
        });

        send.setOnAction(event -> {

            if (from.getText().length() == 0) {
                AlertUtils.alert_warning("发件人不能为空!");
                return;
            }

            if (pass.getText().length() == 0) {
                AlertUtils.alert_warning("发件人密码不能为空!");
                return;
            }

            if (user_list.getItems().size() == 0) {
                AlertUtils.alert_warning("请添加收件人再试!");
                return;
            }

            if (host.getText().length() == 0) {
                host.setText(p + "." + from.getText().split("@")[1]);
            }

            if (port.getText().length() == 0) {
                user.setText(from.getText().split("@")[0]);
            }

            MailAccount account = new MailAccount();
            account.setHost(host.getText());
            account.setPort(Integer.parseInt(port.getText()));
            account.setAuth(true);
            account.setFrom(from.getText());
            account.setUser(user.getText());
            account.setPass(pass.getText());
            account.setSslEnable(true);

            log.info("account:{}",account);

            //发送人列表
            ObservableList<String> items = user_list.getItems();
            List<String> to_list = new ArrayList<>(items);
            //判断附件是否为空
            if (file_info.getValue() != null) {
                MailUtil.send(account,to_list, subject.getText(), content.getText(), false, file_info.getValue());
            } else {
                MailUtil.send(account,to_list, subject.getText(), content.getText(), false);
            }

            AlertUtils.alert_msg("发送成功!");
        });

        add_user_to.setOnAction(event -> {
            if (to.getText().length() == 0) {
                AlertUtils.alert_warning("请输入收件人!");
                return;
            }
            list.add(to.getText());
            to.setText(null);
        });
    }
}
